package tetris.features;

import tetris.simulator.State;

public class FeatureExtraction {

	// return an array of height of each column
	public double[] top(State s) {
		State newState = new State(s);
		int[] IntTop = newState.getTop();
		double[] invtop = new double[IntTop.length];
		
		for (int i=0;i<IntTop.length;i++){
			//invtop[i] = (double) 1/(IntTop[i]+1);
			invtop[i] = (double) IntTop[i];
		}
		
		return invtop;
	}
	
	// return max height 
	public double maxtop(State s) {
		State newState = new State(s);
	
		double maxTop = 0;
		for (int i = 0; i < newState.getTop().length; i++){
			if (newState.getTop()[i] > maxTop)
				maxTop = newState.getTop()[i];				
		}		
		//return 1/maxTop;
		return maxTop;
	}
	
	// return array of size columnSize-1; difference of heights between columns
	public double[] heightDifference(State s) {
		State newState = new State(s);
		int[] height = newState.getTop();
		double[] heightDifference = new double[height.length];
		
		for (int i=0;i<height.length-1;i++){
			heightDifference[i] = (double)height[i+1] -  (double)height[i];
		}
		
		return heightDifference;
	}
	
	
	// return an array of  # of holes present in each column
	public double[] holes(State s) {
		State newState = new State(s);
		
		int[][] stateMat = newState.getField();
		int[] top = newState.getTop();
		double[] holes = new double[top.length]; 
		
		for (int i = 0; i < top.length; i++){
			for (int j = top[i]-1; j > 0; j--){
				if (stateMat[j][i]==0){
					holes[i]+=1;
				}
			}
			//holes[i]=1/(holes[i]+1);
		}
		return holes;
	}
	
	// return an array of size of overhangs in each column
	// CAVEAT: Counts size of just 1 overhang per column (first-come-first serve)
	public double[] overhangs(State s, int[] holes) {
		State newState = new State(s);
		
		int[][] stateMat = newState.getField();
		int[] top = newState.getTop();
		double[] overhangs = new double[top.length]; 
		
		// i = column number
		for (int i = 0; i < top.length; i++){
			// only if more than one hole is present
			if (holes[i]>1){
				int index_new = -1; // current hole index
				int index_old = -1; //last hole index
				// j= row number = start from maxHeight-1
				for (int j = top[i]-1; j > 0; j++){
					// if hole
					if (stateMat[j][i]==0){
						// if no hole appeared till now
						if (index_new==-1 && index_old==-1){
							index_old = j;
							index_new = j;
						}
						else{
							index_old = index_new;
							index_new = j;
						}
						//	update count of overhangs for the current column
						if (index_new-index_old<=1)
							overhangs[i]+=1;
					}
				}
			}
		}
		return overhangs;
	}
	

	public static int featureSize(int c){
		// feature =height
		if (c==0){
			return State.COLS;
		}
		// feature = height and height difference 
		else if(c==1){
			return (2*State.COLS-1);
		}
		// feature = height, height difference and holes
		else if(c==2){
			return (3*State.COLS-1);
		}
		// feature = height, height difference,holes and max height
		else if(c==4){
			return (3*State.COLS);
		}
		// feature = height,holes and max height
		else if(c==5){
			return (2*State.COLS+1);
		}
		// default min features
		else{
			return State.COLS;
		}
		
	}

	
}